<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="0普通发票1电子发票2增值税发票" prop="invoiceType">
      <el-input v-model="dataForm.invoiceType" placeholder="0普通发票1电子发票2增值税发票"></el-input>
    </el-form-item>
    <el-form-item label="发票金额" prop="invoiceMoney">
      <el-input v-model="dataForm.invoiceMoney" placeholder="发票金额"></el-input>
    </el-form-item>
    <el-form-item label="发票抬头" prop="invoiceTitle">
      <el-input v-model="dataForm.invoiceTitle" placeholder="发票抬头"></el-input>
    </el-form-item>
    <el-form-item label="发票内容" prop="invoiceDesc">
      <el-input v-model="dataForm.invoiceDesc" placeholder="发票内容"></el-input>
    </el-form-item>
    <el-form-item label="发票税率" prop="invoiceRate">
      <el-input v-model="dataForm.invoiceRate" placeholder="发票税率"></el-input>
    </el-form-item>
    <el-form-item label="纳税人识别号" prop="taxpayer">
      <el-input v-model="dataForm.taxpayer" placeholder="纳税人识别号"></el-input>
    </el-form-item>
    <el-form-item label="发票状态0待开1已开2作废" prop="status">
      <el-input v-model="dataForm.status" placeholder="发票状态0待开1已开2作废"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createOn">
      <el-input v-model="dataForm.createOn" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifiedOn">
      <el-input v-model="dataForm.modifiedOn" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifiedUserId">
      <el-input v-model="dataForm.modifiedUserId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modifiedBy">
      <el-input v-model="dataForm.modifiedBy" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          userId: '',
          invoiceType: '',
          invoiceMoney: '',
          invoiceTitle: '',
          invoiceDesc: '',
          invoiceRate: '',
          taxpayer: '',
          status: '',
          createOn: '',
          createUserId: '',
          createBy: '',
          modifiedOn: '',
          modifiedUserId: '',
          modifiedBy: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          invoiceType: [
            { required: true, message: '0普通发票1电子发票2增值税发票不能为空', trigger: 'blur' }
          ],
          invoiceMoney: [
            { required: true, message: '发票金额不能为空', trigger: 'blur' }
          ],
          invoiceTitle: [
            { required: true, message: '发票抬头不能为空', trigger: 'blur' }
          ],
          invoiceDesc: [
            { required: true, message: '发票内容不能为空', trigger: 'blur' }
          ],
          invoiceRate: [
            { required: true, message: '发票税率不能为空', trigger: 'blur' }
          ],
          taxpayer: [
            { required: true, message: '纳税人识别号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '发票状态0待开1已开2作废不能为空', trigger: 'blur' }
          ],
          createOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedOn: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedUserId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modifiedBy: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bid/invoice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.invoice.orderId
                this.dataForm.userId = data.invoice.userId
                this.dataForm.invoiceType = data.invoice.invoiceType
                this.dataForm.invoiceMoney = data.invoice.invoiceMoney
                this.dataForm.invoiceTitle = data.invoice.invoiceTitle
                this.dataForm.invoiceDesc = data.invoice.invoiceDesc
                this.dataForm.invoiceRate = data.invoice.invoiceRate
                this.dataForm.taxpayer = data.invoice.taxpayer
                this.dataForm.status = data.invoice.status
                this.dataForm.createOn = data.invoice.createOn
                this.dataForm.createUserId = data.invoice.createUserId
                this.dataForm.createBy = data.invoice.createBy
                this.dataForm.modifiedOn = data.invoice.modifiedOn
                this.dataForm.modifiedUserId = data.invoice.modifiedUserId
                this.dataForm.modifiedBy = data.invoice.modifiedBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bid/invoice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'userId': this.dataForm.userId,
                'invoiceType': this.dataForm.invoiceType,
                'invoiceMoney': this.dataForm.invoiceMoney,
                'invoiceTitle': this.dataForm.invoiceTitle,
                'invoiceDesc': this.dataForm.invoiceDesc,
                'invoiceRate': this.dataForm.invoiceRate,
                'taxpayer': this.dataForm.taxpayer,
                'status': this.dataForm.status,
                'createOn': this.dataForm.createOn,
                'createUserId': this.dataForm.createUserId,
                'createBy': this.dataForm.createBy,
                'modifiedOn': this.dataForm.modifiedOn,
                'modifiedUserId': this.dataForm.modifiedUserId,
                'modifiedBy': this.dataForm.modifiedBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
